package com.example.industryapp.control;

import com.example.industryapp.dao.OrderDAO;
import com.example.industryapp.dataobject.OrderDO;
import com.example.industryapp.dataobject.UserDO;
import com.example.industryapp.model.Order;
import com.example.industryapp.model.Paging;
import com.example.industryapp.model.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class OrderController {
    @Autowired
    private OrderDAO orderDAO;
    @GetMapping("/orders")
    @ResponseBody
    public Result<Paging<OrderDO>> getAll(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Result<Paging<OrderDO>> result = new Result();
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        // 设置当前页数为1，以及每页3条记录
        Page<OrderDO> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> orderDAO.findAll());

        result.setSuccess(true);
        result.setData(
                new Paging<>(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page.getResult()));
        return result;

    }
    @PostMapping("/order")
    @ResponseBody
    public OrderDO save(@RequestBody OrderDO orderDO) {
        orderDAO.add(orderDO);
        return orderDO;
    }
    @PostMapping("/order/update")
    @ResponseBody
    public OrderDO update(@RequestBody OrderDO orderDO){
        orderDAO.update(orderDO);
        return orderDO;
    }
    @GetMapping("/order/del")
    @ResponseBody
    public Boolean delete(@RequestParam("id") Long id){
        return orderDAO.delete(id)>0;

    }

}
